layui.use(['form', 'util', 'upload'], function () {
    var upload = layui.upload;
    var uploadButtons = document.getElementsByClassName('upload');
    $.each(uploadButtons, function (index, button) {
        var input = button.getAttribute('data-input-id');
        var preview = button.getAttribute('data-preview-id');
        var multiple = button.getAttribute('data-multiple');
        var uploadUrl = button.getAttribute('data-upload-url');
        var accept = button.getAttribute('data-accept');
        var acceptMime = button.getAttribute('data-accept-mime');
        var cut = button.getAttribute('data-cut');
        accept = typeof accept === '' || typeof accept === undefined || typeof accept === null ? 'image' : accept;
        acceptMime = typeof acceptMime === '' || typeof acceptMime === undefined || typeof acceptMime === null ? 'image/*' : acceptMime;
        multiple = typeof multiple === '' || typeof multiple === undefined || typeof multiple === null ? false : multiple;
        cut = typeof cut === '' || typeof cut === undefined || typeof cut === null|| typeof cut == 'false' ? false : cut;
        var uploadRender = upload.render({
            elem: button,
            data: {cut: cut},
            url: uploadUrl,
            auto: false,
            multiple: multiple,
            choose: function (obj) {
                var flag = true;
                layer.load();
                obj.preview(function (index, file, result) {
                    if (uploadUrl === null) {
                        layer.msg('上传地址不能为空');
                        layer.closeAll('loading');
                        flag = false;
                    } else {
                        //对上传失败的单个文件重新上传，一般在某个事件中使用
                        obj.upload(index, file);
                    }
                    uploadRender.config.elem.next()[0].value = '';
                    return flag;
                });
            },
            done: function (res, index, upload) { //上传后的回调
                var imgHtml = "";
                var data = res;
                if (multiple == 'false') {
                    if (/[;]/.test(data.cdn_url)) {
                        var item2 = data.cdn_url.split(';');
                        $.each(item2, function (index2, value2) {
                            if (value2 !== '') {
                                imgHtml += "<img src='" + value2 + "' class='layui-upload-img' width='150' style='display: inherit;margin:0px;'/>";
                            }
                        });
                    }else{
                        imgHtml = "<img src='" + data.cdn_url + "' class='layui-upload-img' width='150'/>";
                    }
                    $('#' + input).val(data.vkey);
                    $('#' + preview).html(imgHtml);
                } else {
                    var imagesValue = $('#' + input).val();
                    var imagesArray = [data.vkey];
                    var url = '';
                    if (imagesValue !== '') {
                        var item = imagesValue.split(';');
                        $.each(item, function (index, value) {
                            if (value !== '') {
                                imagesArray.push(value);
                            }
                        });
                    }
                    imagesArray = imagesArray.filter(function (item, index, arr) {
                        //当前元素，在原始数组中的第一个索引==当前索引值，否则返回当前元素
                        return arr.indexOf(item, 0) === index;
                    });
                    $.each(imagesArray, function (index, value) {
                        url += value + ';';
                    })
                    imgHtml = "<img src='" + data.cdn_url + "' class='layui-upload-img' width='150' height='150'/>";
                    $('#' + input).val(url);
                    $('#' + preview).append(imgHtml);
                }
                layer.closeAll('loading');

            },
            error: function (index, upload) {
                layer.closeAll('loading');
                layer.msg('上传失败');
            },
            accept: accept,
            acceptMime: acceptMime,
        })
    })

});